package o2o.web.actions.sys;

import com.jinyou.utils.common.*;
import o2o.cache.sys.SysCustomerCache;
import o2o.cache.system.SysSettingCategoryMemory;
import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.sys.SysCustomerInfo;
import o2o.dao.PO.sys.SysSettingCategory;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.sys.SysSettingCategoryVO;
import o2o.system.SysSettings;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//系统设置分组管理

@Controller
public class SysSettingsCategoryAction {
    //获取分组
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/settings/category/list")
    @ResponseBody
    public String category_list(String apiKey, String sysAppKey, Integer type, String isShow) {
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }
            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        List<SysSettingCategory> list = SysSettingCategoryMemory.getInstance().listOfType(type);
        if (ValidateUtil.isAbsList(list)) {
            List<SysSettingCategoryVO> voList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }
                SysSettingCategoryVO vo = new SysSettingCategoryVO();
                try {
                    ObjectUtils.copyPropertiesExclude(list.get(i), vo, null);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                if (isShow != null && isShow != "") {
                    if (String.valueOf(list.get(i).getIsShow()).equals(isShow)) {
                        voList.add(vo);
                    }
                } else {
                    voList.add(vo);
                }
            }
            // 排序
//            Collections.sort(voList); // 按照isShow降序
            JYListSort.sort(voList,"code");
            if (ValidateUtil.isAbsList(voList)) {
                return AjaxResult.successResult(voList.size(), voList);
            }
        }

        return AjaxResult.successResult(0, null);
    }

    //获取分组
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/settings/category/list/xl")
    @ResponseBody
    public String category_xlList(String apiKey, String sysAppKey, Integer type, String isShow) {
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }
            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        List<SysSettingCategory> list = SysSettingCategoryMemory.getInstance().listOfType(type);
        if (ValidateUtil.isAbsList(list)) {
            List<SysSettingCategoryVO> voList = new ArrayList<>();
            SysSettingCategoryVO sscVo = new SysSettingCategoryVO();
            sscVo.setCode("");
            sscVo.setName("全部");
            sscVo.setIsShow(1);
            voList.add(sscVo);
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }
                SysSettingCategoryVO vo = new SysSettingCategoryVO();
                try {
                    ObjectUtils.copyPropertiesExclude(list.get(i), vo, null);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                voList.add(vo);
            }
            // 排序
            Collections.sort(voList); // 按照isShow降序
            if (ValidateUtil.isAbsList(voList)) {
                return AjaxResult.successResult(voList.size(), voList);
            }
        }

        return AjaxResult.successResult(0, null);
    }

    //  添加分组
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/settings/category/add")
    @ResponseBody
    public String add(String token, String apiKey, String sysAppKey, SysSettingCategory info) {
        UserInfo user = new UserInfo();
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }

            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }
            user.setUsername("admin");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }
        if (ValidateUtil.isNull(info.getName())) {
            return AjaxResult.errorResult("名称不能为空");
        }
        try {
//            SysSettingDB pc = new SysSettingDB();
//            ObjectUtils.copyPropertiesExclude(info, pc, null);
            info.setCreateUser("admin");
            info.setCreateTime(DateUtils.getCurrTime());
            UserDaoSql.getInstance().insertObj(PojoTables.SYS_SETTING_CATEGORY, info);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("新增失败，请稍后再试");
        }
        SysSettings.init();//重新初始化系统值
        return AjaxResult.successResult();
    }

    //  修改分组
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/settings/category/modify")
    @ResponseBody
    public String modify(String token, String apiKey, String sysAppKey, SysSettingCategory info) {
        UserInfo user = new UserInfo();
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }

            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }
            user.setUsername("admin");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }
        if (ValidateUtil.isNull(info.getName())) {
            return AjaxResult.errorResult("名称不能为空");
        }
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.SYS_SETTING_CATEGORY).append(" set name='").append(info.getName())
                .append("',descs='").append(info.getDescs())
                .append("',descs='").append(info.getDescs())
                .append("',isShow='").append(info.getIsShow())
                .append("',note='").append(info.getNote())
                .append("',updateUser='").append(user.getUsername())
                .append("' where  code = '").append(info.getCode())
                .append("'  and  sysAppKey = '").append(info.getSysAppKey()).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        //修改缓存
        SysSettings.init();//重新初始化系统值
        return AjaxResult.successResult();
    }

    //  删除分组
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/settings/category/delete")
    @ResponseBody
    public String delete(String token, String apiKey, String sysAppKey, SysSettingCategory info) {
        UserInfo user = new UserInfo();
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }

            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }
            user.setUsername("admin");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.SYS_SETTING_CATEGORY)
                .append(" set delFlag='").append(info.getDelFlag())
                .append("',updateUser='").append(user.getUsername())
                .append("' where  id = '").append(info.getId())
                .append("'  and  sysAppKey = '").append(info.getSysAppKey()).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("删除失败，请稍后再试");
        }

        //修改缓存
        SysSettings.init();//重新初始化系统值
        return AjaxResult.successResult();
    }
}
